package factory.kotlinMode.abstractFactory

import factory.kotlinMode.Computer
import java.lang.IllegalArgumentException

interface AbstractFactory {
    //创建产品
    fun create(): Computer

    companion object {

        /**
         * 使用内联函数和reified关键字,可以拿到泛型的类型
         */
        inline operator fun <reified T : Computer> invoke(): AbstractFactory =
            when (T::class) {
                DELL::class -> DELLFactory()
                HP::class -> HPFactory()
                IBM::class -> IBMFactory()
                else -> throw IllegalArgumentException()
            }

        /**
         * 使用伴生对象加操作符重载创建工厂
         */
        operator fun invoke(factory: AbstractFactory): AbstractFactory = factory
    }
}